package blog.db;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import blog.util.*;

public class BlogEntry implements IStorableRecordWithIndex {
	
	public static final int FIELD_ACCOUNT_ID = 1;
	public static final int FIELD_TITLE = 2;
	public static final int FIELD_BODY = 3;
	public static final int FIELD_CREATED_DATE = 4;
	public static final int FIELD_ALLOW_COMMENTS = 5;
	public static final int FIELD_PUBLISH = 6;
	public static final int FIELD_CONVERT_BREAKS = 7;
	public static final int FIELD_PUBLISH_OK = 8;
	public static final int FIELD_IMAGE_DATA = 9;
	public static final int FIELD_IMAGE_NAME = 10;
	public static final int FIELD_IMAGE_URL = 11;

	private int accountId;
	private String title;
	private String body;
	private long createdDate;
	private boolean allowComments;
	private boolean publish;
	private boolean convertBreaks;
	private boolean publishOk;
	private byte[] mediaData;
	private String mediaName;
	private String mediaUrl;
	private String mediaType;
	
	private int m_id;
	private int m_indexId;
	
	public BlogEntry()
	{
		accountId = -1;
		title = "";
		body = "";
		createdDate = System.currentTimeMillis();
		allowComments = false;
		publish = true;
		convertBreaks = false;
		m_id = -1;
		m_indexId = -1;
		publishOk = false;
		mediaData = null;
		mediaName = "";
		mediaUrl = "";
		mediaType = "";
	}

	public int compare(int field, IStorableRecordWithIndex another) {
		BlogEntry tmpObj = (BlogEntry) another;
		switch( field )
		{
		case FIELD_ACCOUNT_ID:
			return accountId - tmpObj.accountId;
		case FIELD_TITLE:
			return title.compareTo(tmpObj.title);
		case FIELD_BODY:
			return body.compareTo(tmpObj.body);
		case FIELD_CREATED_DATE:
		{
			if ( createdDate>tmpObj.createdDate )
			{
				return 1;
			}
			else if ( createdDate<tmpObj.createdDate )
			{
				return -1;
			}
			else
			{
				return 0;
			}
		}
		}
		return 0;
	}

	public int compareField(int field, Object fieldValue) {
		switch( field )
		{
		case FIELD_ACCOUNT_ID:
		{
			return this.accountId-((Integer) fieldValue).intValue();
		}
		case FIELD_TITLE:
		{
			return this.title.compareTo((String)fieldValue);
		}
		case FIELD_CREATED_DATE:
		{
			return (int) (this.createdDate - ((Long)fieldValue).longValue());
		}
		case FIELD_PUBLISH_OK:
		{
			boolean fieldV = ((Boolean)fieldValue).booleanValue();
			if ( fieldV==this.publishOk )
			{
				return 0;
			}
			else if ( fieldV && !publishOk )
			{
				return -1;
			}
			else 
			{
				return 1;
			}
		}
		}
		return 0;
	}

	public void deserializeIndex(DataInputStream dis) throws IOException {
		this.accountId = dis.readInt();
		this.title = dis.readUTF();
		createdDate = dis.readLong();
		allowComments = dis.readBoolean();
		this.publish = dis.readBoolean();
		this.convertBreaks = dis.readBoolean();
		this.publishOk = dis.readBoolean();
	}

	public int getIndexId() {
		return m_indexId;
	}

	public boolean isIndexField(int field) {
		return (field==FIELD_ACCOUNT_ID) || (field==FIELD_TITLE) || (field==FIELD_CREATED_DATE)
			|| (field==FIELD_ALLOW_COMMENTS) || (field==FIELD_PUBLISH) || (field==FIELD_CONVERT_BREAKS);
	}

	public void serializeIndex(DataOutputStream dos) throws IOException {
		dos.writeInt( this.accountId );
		dos.writeUTF( StringUtils.null2empty(this.title) );
		dos.writeLong( createdDate );
		dos.writeBoolean( allowComments );
		dos.writeBoolean( publish );
		dos.writeBoolean( convertBreaks );
		dos.writeBoolean( this.publishOk );
	}

	public void setIndexId(int indexId) {
		this.m_indexId = indexId;
	}

	public void deserialize(DataInputStream dis) throws IOException {
		this.deserializeIndex(dis);
		this.body = dis.readUTF();
		int len = dis.readInt(), readLen = 0;
		if ( len>0 )
		{
			this.mediaData = new byte[len];
			while ( readLen<len )
			{
				readLen += dis.read(mediaData, readLen, len-readLen);
			}
		}
		this.mediaName = dis.readUTF();
		mediaUrl = dis.readUTF();
		mediaType = dis.readUTF();
	}

	public int getId() {
		return this.m_id;
	}

	public void serialize(DataOutputStream dos) throws IOException {
		this.serializeIndex(dos);
		dos.writeUTF( StringUtils.null2empty(this.body) );
		if ( this.mediaData==null || mediaData.length==0 )
		{
			dos.writeInt(0);
		}
		else
		{
			dos.writeInt(mediaData.length);
			dos.write(mediaData);
		}
		dos.writeUTF( StringUtils.null2empty(this.mediaName) );
		dos.writeUTF( StringUtils.null2empty(this.mediaUrl) );
		dos.writeUTF( StringUtils.null2empty(this.mediaType) );
	}

	public void setId(int id) {
		this.m_id = id;
	}

	public int getAccountId() {
		return accountId;
	}

	public void setAccountId(int accountId) {
		this.accountId = accountId;
	}

	public boolean isAllowComments() {
		return allowComments;
	}

	public void setAllowComments(boolean allowComments) {
		this.allowComments = allowComments;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public boolean isConvertBreaks() {
		return convertBreaks;
	}

	public void setConvertBreaks(boolean convertBreaks) {
		this.convertBreaks = convertBreaks;
	}

	public boolean isPublish() {
		return publish;
	}

	public void setPublish(boolean publish) {
		this.publish = publish;
	}

	public long getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(long publishDate) {
		this.createdDate = publishDate;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public boolean isPublishOk() {
		return publishOk;
	}

	public void setPublishOk(boolean publishOk) {
		this.publishOk = publishOk;
	}

	public byte[] getMediaData() {
		return mediaData;
	}

	public void setMediaData(byte[] imageData) {
		this.mediaData = imageData;
	}

	public String getMediaName() {
		return mediaName;
	}

	public void setMediaName(String imageName) {
		this.mediaName = imageName;
	}

	public String getMediaUrl() {
		return mediaUrl;
	}

	public void setMediaUrl(String imageUrl) {
		this.mediaUrl = imageUrl;
	}

	public String getMediaType() {
		return mediaType;
	}

	public void setMediaType(String imageType) {
		this.mediaType = imageType;
	}
}